import 'dart:developer';

import 'package:edu/constants/app_strings.dart';
import 'package:edu/utils/data_fetcher.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class PasswordSettingPageLogic extends ChangeNotifier {
  late final passwordController0 = TextEditingController();
  late final passwordController1 = TextEditingController();
  late final passwordController2 = TextEditingController();

  late final focusNode0 = FocusNode();
  late final focusNode1 = FocusNode();
  late final focusNode2 = FocusNode();

  bool? password0Ready;
  bool? password1Ready;
  bool? password2Ready;

  bool _password0Used = false;
  bool _password1Used = false;
  bool _password2Used = false;

  bool get canSubmit =>
      password0Ready == true &&
      password1Ready == true &&
      password2Ready == true;

  void init() {
    passwordController0.addListener(() {
      final text0 = passwordController0.text;
      if (text0.isNotEmpty) {
        _password0Used = true;
      }
      if (_password0Used) {
        password0Ready = text0.length >= 8;
      }
      notifyListeners();
    });
    passwordController1.addListener(() {
      final text1 = passwordController1.text;
      if (text1.isNotEmpty) {
        _password1Used = true;
      }

      if (_password1Used) {
        password1Ready = text1.length >= 8;
      }

      if (_password2Used) {
        final text2 = passwordController2.text;
        password2Ready = text2.length >= 8 && text1 == text2;
      }
      notifyListeners();
    });
    passwordController2.addListener(() {
      final text = passwordController2.text;
      if (text.isNotEmpty) {
        _password2Used = true;
      }
      if (_password2Used) {
        password2Ready = text.length >= 8 && text == passwordController1.text;
      }
      notifyListeners();
    });
  }

  // 访问后端接口
  Future<bool> submit() async {
    focusNode0.unfocus();
    focusNode1.unfocus();
    focusNode2.unfocus();

    final oldPassword = passwordController0.text;
    final newPassword = passwordController1.text;
    final confirmPassword = passwordController2.text;

    // 新密码和旧密码一致
    if (oldPassword == newPassword) {
      EasyLoading.showError(AppStrings.newAndOldPasswordSame);
      return false;
    }
    // 确认密码和新密码不一致
    if (newPassword != confirmPassword) {
      return false;
    }

    try {
      EasyLoading.show(
        indicator: const CircularProgressIndicator(),
        maskType: EasyLoadingMaskType.black,
        dismissOnTap: true,
      );

      // 访问后端接口
      final response = await DataFetcher.shared.userApi.updateUserPassword(
        type: 'old_password',
        password: newPassword,
        code: oldPassword,
      );
      EasyLoading.dismiss();

      // 响应成功
      if (response.code == 0) {
        EasyLoading.showSuccess(AppStrings.updateSuccess);
        return true;
      } else {
        EasyLoading.showError(response.message ?? AppStrings.unknownError);
        return false;
      }
    } catch (e) {
      EasyLoading.dismiss();
      EasyLoading.showError(e.toString());
      log('Error:$e');
      return false;
    }
  }
}
